#include "LabelStatus.h"
#include "../../common/utils/UtilString.h"


LabelStatus::LabelStatus(std::string message, Widget* parent, int x, int y) :
	Widget(x, y, NULL, parent, true) {

	this->textSurface = SurfaceUtils::renderText(message);
	this->surface = SurfaceUtils::colorSurface(GAME_MENU_WIDTH,STATUS_HEIGHT,Color::GRIS);

	delete this->parcel;
	initParcel(x,y);
}

void LabelStatus::showOn(SDL_Surface* surface) {
	SDL_Rect textRect;
	textRect.w = textSurface->w;
	textRect.h = textSurface->h;

	if(this->surface){
		textRect.x = this->parcel->posX*0.1;
		textRect.y = (this->parcel->height/2) - (textRect.h /2) ;
		SDL_BlitSurface(this->textSurface, NULL, this->surface, &textRect);
	}else{
		textRect.x = this->parcel->posX*0.1;
		textRect.y = this->parcel->posY - this->textSurface->h /2;
		SDL_BlitSurface(this->textSurface, NULL, surface, &textRect);
	}

	Widget::showOn(surface);
}


Parcel* LabelStatus::getBoundaries(){
	if(this->surface == NULL)
		return SurfaceUtils::getBoundaries(this->textSurface);

	return Widget::getBoundaries();
}

void LabelStatus::setText(std::string message){
	delete this->textSurface;
	delete this->surface;
	this->surface = SurfaceUtils::colorSurface(GAME_MENU_WIDTH,STATUS_HEIGHT,Color::GRIS);
	this->textSurface = SurfaceUtils::renderText(message);
}

void LabelStatus::handleEvent(Event event) {

	if (event.getType() == UserEvent) {
		if(event.getCode() == SERVER_CREATED) {
			this->setText("Servidor Creado");
		}
		if(event.getCode() == SERVER_ERROR) {
			this->setText("Otra instancia de server esta corriendo.");
		}
		if(event.getCode() == CLIENT_CREATED) {
			this->setText("Cliente conectado. Esperando contrincante...");
		}
		if(event.getCode() == CLIENT_ERROR) {
			this->setText("Error al conectarse con el servidor.");
		}
		if(event.getCode() == CLIENT_CONNECTING) {
			this->setText("Conectando...");
		}
	}

	Widget::handleEvent(event);
}

LabelStatus::~LabelStatus() {
	SDL_FreeSurface(textSurface);
}
